import type { App } from 'vue'
import {createI18n} from 'vue-i18n'
import {useLocaleStoreWithOut} from '@/stores/modules/locales'
import type {I18n,I18nOptions} from 'vue-i18n'
import {setHtmlPageLang} from './helper'
// const langModules = import.meta.glob('./lang/*.ts', { eager: true })
export let i18n:ReturnType<typeof createI18n>
const createI18nOptions = async():Promise<I18nOptions>=>{
    const localeStore = useLocaleStoreWithOut()
    const locale = localeStore.getCurrentLocale
    const localeMap=localeStore.getLocaleMap
    const defaultLocal = await import(`../../locales/${locale.lang}.ts`)
    const message = defaultLocal.default?.message ?? {}
    setHtmlPageLang(locale.lang)
    localeStore.setCurrentLocale({
        lang: locale.lang
        // elLocale: elLocal
      })
    return {
        legacy:false,
        locale:locale.lang,
        fallbackLocale:locale.lang,
        messages:{
            [locale.lang]:message
        },
        availableLocales:localeMap.map(item=>item.lang),
        sync:true,
        silentTranslationWarn:true,
        missingWarn:false,
    }
}
export const setupI18n = async (app: App<Element>) => {
    const options = await createI18nOptions()
    i18n = createI18n<false>(options) as I18n
    app.use(i18n)
}
  